﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

using Perseus.Data;

using Hetymine.Executioner.Data;

namespace Hetymine.Executioner.Plugins.General.Imgur {
    /// <summary>
    /// Interaction logic for ImgurConfigurator.xaml
    /// </summary>
    public partial class ImgurConfigurator : UserControl, IConfigurator<IniFile> {
        private IniFile _Config;

        public ImgurConfigurator() {
            InitializeComponent();

            this.cImageDirectory.TextChanged += new TextChangedEventHandler(ImageDirectory_TextChanged);
            this.cCopyToClipboard.Click += new RoutedEventHandler(CopyToClipboard_Click);
            this.cCopyUrl.SelectionChanged += new SelectionChangedEventHandler(CopyUrl_SelectionChanged);
            this.cOpenUrlInBrowser.Click += new RoutedEventHandler(OpenUrlInBrowser_Click);
            this.cOpenUrl.SelectionChanged += new SelectionChangedEventHandler(OpenUrl_SelectionChanged);
            this.cLogImgurPage.Click += new RoutedEventHandler(LogUrls_Click);
            this.cLogOrigionalImage.Click += new RoutedEventHandler(LogUrls_Click);
            this.cLogLargeThumbnail.Click += new RoutedEventHandler(LogUrls_Click);
            this.cLogSmallThumbnail.Click += new RoutedEventHandler(LogUrls_Click);
            this.cLogDeletePage.Click += new RoutedEventHandler(LogUrls_Click);
            this.cDeleteLogs.Click += new RoutedEventHandler(DeleteLogs_Click);
        }

        private void ImageDirectory_TextChanged(object sender, TextChangedEventArgs e) {
            this._Config.SetString("Imgur", "Default Image Directory", this.cImageDirectory.Text.Trim());
        }
        private void CopyToClipboard_Click(object sender, RoutedEventArgs e) {
            this._Config.SetBool("Imgur", "Copy To Clipboard", this.cCopyToClipboard.IsChecked.Value);
        }
        private void CopyUrl_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            this._Config.SetInt("Imgur", "Copy To Clipboard Page", this.cCopyUrl.SelectedIndex);
        }
        private void OpenUrlInBrowser_Click(object sender, RoutedEventArgs e) {
            this._Config.SetBool("Imgur", "Open Url In Browser", this.cOpenUrlInBrowser.IsChecked.Value);
        }
        private void OpenUrl_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            this._Config.SetInt("Imgur", "Open Url In Browser Page", this.cOpenUrl.SelectedIndex);
        }
        private void LogUrls_Click(object sender, RoutedEventArgs e) {
            int log = 0;

            // Store logged urls as a flag
            if (this.cLogImgurPage.IsChecked.Value) {
                log |= 1;
            }
            if (this.cLogOrigionalImage.IsChecked.Value) {
                log |= 2;
            }
            if (this.cLogLargeThumbnail.IsChecked.Value) {
                log |= 4;
            }
            if (this.cLogSmallThumbnail.IsChecked.Value) {
                log |= 8;
            }
            if (this.cLogDeletePage.IsChecked.Value) {
                log |= 16;
            }

            this._Config.SetInt("Imgur", "Log Pages", log);
        }
        private void DeleteLogs_Click(object sender, RoutedEventArgs e) {
            string file = AppDomain.CurrentDomain.BaseDirectory +
                          "plugins" + Path.DirectorySeparatorChar +
                          "Imgur" + Path.DirectorySeparatorChar +
                          "log.txt";

            if (File.Exists(file)) {
                File.Delete(file);
            }

            this.cDeleteLogs.IsEnabled = false;
        }

        #region IConfigurator<IniFile>
        public void InitializeConfig(IniFile config) {
            this._Config = config;

            this.cImageDirectory.Text = this._Config.GetString(
                "Imgur",
                "Default Image Directory",
                Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
            );

            this.cCopyToClipboard.IsChecked = this._Config.GetBool(
                "Imgur",
                "Copy To Clipboard",
                true
            );
            this.cCopyUrl.SelectedIndex = this._Config.GetInt(
                "Imgur",
                "Copy To Clipboard Page",
                0
            );

            this.cOpenUrlInBrowser.IsChecked = this._Config.GetBool(
                "Imgur",
                "Open Url In Browser",
                false
            );
            this.cOpenUrl.SelectedIndex = this._Config.GetInt(
                "Imgur",
                "Open Url In Browser Page",
                0
            );

            int log = this._Config.GetInt(
                "Imgur",
                "Log Pages",
                0
            );

            this.cLogImgurPage.IsChecked = ((log | 1) == log);
            this.cLogOrigionalImage.IsChecked = ((log | 2) == log);
            this.cLogLargeThumbnail.IsChecked = ((log | 4) == log);
            this.cLogSmallThumbnail.IsChecked = ((log | 8) == log);
            this.cLogDeletePage.IsChecked = ((log | 16) == log);

            string file = AppDomain.CurrentDomain.BaseDirectory +
                          "plugins" + Path.DirectorySeparatorChar +
                          "Imgur" + Path.DirectorySeparatorChar +
                          "log.txt";

            this.cDeleteLogs.IsEnabled = File.Exists(file);
        }
        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
